package com.ccys.app.controller;

import cn.hutool.core.util.IdUtil;
import com.ccys.common.third.mailGun.MailgunUtil;
import com.ccys.common.util.HtmlUtils;
import com.ccys.common.vo.ResponseResultApp;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.app.shareChildScreen.ShareChildScreenDto;
import com.ccys.db.entity.TblMember;
import com.ccys.db.entity.TblMemberFamily;
import com.ccys.db.mapper.TblMemberFamilyMapper;
import com.ccys.db.mapper.TblMemberMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @Author：XLY
 * @Date：2024/2/1 0001
 */
@RestController
@RequestMapping("/shareChildScreen")
@Api(tags = "Share Child Screen")
@Slf4j
public class ShareChildScreenController {

    @Resource
    private TblMemberFamilyMapper familyMapper;

    @Resource
    private TblMemberMapper memberMapper;

    @ApiOperation("Share Child Screen/1")
    @PostMapping("ShareChild")
    @Transactional
    public ResponseResultApp shareChild(@RequestBody ShareChildScreenDto dto) {
        //根据邮箱查到member信息
        String email = dto.getEmail();
        if (StringUtils.isNotEmpty(email)) {
            Example example = new Example(TblMember.class);
            example.createCriteria().andEqualTo("email", email);
            List<TblMember> memberList = memberMapper.selectByExample(example);
            if (CollectionUtils.isNotEmpty(memberList)) {
                TblMember member = memberList.get(0);
                //往family里面添加一条数据
                TblMemberFamily memberFamily = new TblMemberFamily();
                memberFamily.setMemberId(member.getId());
                memberFamily.setRelationship(dto.getRelationship());
                memberFamily.setCode(IdUtil.getSnowflakeNextIdStr());
                memberFamily.setStatus(1);
                memberFamily.setDependantId(dto.getChild_id());
                memberFamily.setInvitedEmail(dto.getEmail());
                String curLoginUserId = UserUtil.getCurLoginUserId();
                memberFamily.setMemberIdSendInvitation(curLoginUserId);
                familyMapper.insert(memberFamily);
                // TODO: 2024/2/1 0001  给邀请人和被邀请人发送邮件
                TblMember curLoginMember = UserUtil.getCurLoginMember();
                if (Objects.nonNull(curLoginMember)) {
                    String senderName = curLoginMember.getFirstName() + ' ' + curLoginMember.getLastName();
                    Integer curLoginMemberGender = curLoginMember.getGender();
                    String gender = (Objects.nonNull(curLoginMemberGender) && curLoginMemberGender == 0) ? "her" : "his";

                    CompletableFuture.runAsync(() -> {
                        try {
                            //发送给被邀请人
                            Map<String, Object> map = new HashMap<>();
                            map.put("senderName", senderName);
                            map.put("gender", gender);
                            String templateContent = HtmlUtils.getTemplateContent("Sharing_Child_Been_Invitation.ftl", map);
                            MailgunUtil.sendEmail(dto.getEmail(), "You Have Been Invited!", null, templateContent);

                            //发送给邀请人
                            String invitorTemplateContent = HtmlUtils.getTemplateContent("Sharing_Child_Invitation.ftl", map);
                            MailgunUtil.sendEmail(curLoginMember.getEmail(), "Your Invitation Has Been Sent!", null, invitorTemplateContent);

                        } catch (Exception e) {
                            log.error(e.getMessage());
                        }
                    });
                }
            }
        }
        return ResponseResultApp.success();
    }

}
